﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace snat.algorithms.node.TFIDF {
    public class IDF {

        private Dictionary<String, double> score;
        public double this[String s] {
            get {
                return score[s];
            }

        }

        private Dictionary<String, int> wordCount;
        public Dictionary<String, int> WordCount {
            get {
                return wordCount;
            }
        }

        private Dictionary<String, int> appearenceCount;
        public Dictionary<String, int> AppearenceCount {
            get {
                return appearenceCount;
            }
        }

        public int TotalWords { get; private set; }
        public int TotalDocuments { get; private set; }

        public IDF() {
            this.score = new Dictionary<String, double>();
            this.wordCount = new Dictionary<String, int>();
            this.appearenceCount = new Dictionary<String, int>();
            TotalWords = 0;
            TotalDocuments = 0;
        }

        public void updateScore(Dictionary<String, int> words, int wordsInDoc) {
            TotalDocuments++;
            TotalWords += wordsInDoc;
            foreach (String s in words.Keys) {
                wordCount[s] = wordCount.ContainsKey(s) ? wordCount[s] + words[s] : words[s];
                appearenceCount[s] = appearenceCount.ContainsKey(s) ? appearenceCount[s] + 1 : 1;
                score[s] = Math.Log10(TotalDocuments / appearenceCount[s]);
            }
        }
    }
}
